package com.vast.ui.component.toolbar;

import java.util.concurrent.Executor;

import org.eclipse.swt.widgets.Composite;

import com.vast.ui.component.controller.TableViewerController;
import com.vast.ui.component.socketinterface.listner.IReceiveListener;
import com.vast.ui.component.socketinterface.listner.ISocketConnectionListener;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.socketinterface.server.ServerService;
import com.vast.ui.component.socketinterface.server.IServer;
import com.vast.ui.component.state.Sequence;
import com.vast.ui.component.util.ConsoleLoggerSingleton;
import com.vast.ui.component.util.Logger;
import com.vast.ui.panel.fullcolorled.Activator;
import com.vast.ui.panel.fullcolorled.preferences.PreferenceConstants;
import com.vast.ui.panel.fullcolorled.views.ViewPart;

public class InterfaceController  extends ToolBarAction implements ISocketConnectionListener{
	public  TableViewerController controller;
	private IServer server;
	private final RequestQueue sendQueue;
	public  final Logger     logger;
	
	public InterfaceController(ViewPart view, Composite parent, int serverPort) {
		super(view);		
		this.logger     = ConsoleLoggerSingleton.getInstance();
		sendQueue               = new RequestQueue();
		Sequence sequence       = new Sequence();
		server                  = new ServerService(serverPort, sequence, sendQueue);
		controller = new TableViewerController(parent, sendQueue);
		server.addReceiveListener(controller);
		server.addSocketConnectionListener(this);
	}
	
	public void addReceiveListener(IReceiveListener listener){
		server.addReceiveListener(listener);
	}
	
	public void removeReceiveListener(IReceiveListener listener) {
		server.removeReceiveListener(listener);
	}

	public void addAction() {
		// TODO Auto-generated method stub
		int itnitialValue = Integer.decode("0x" + Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_RESET_VALUE));
		controller.add(itnitialValue);
	}

	public void deleteAction() {
		// TODO Auto-generated method stub
		controller.delete();
	}

	public void startAction() {
		// TODO Auto-generated method stub
		server.openConnection();
		setStopActionEnable();
	}

	public void stopAction() {
		// TODO Auto-generated method stub
		server.closeConnection();
		setStartActionEnable();
	}

	public void notifyStateChange(Sequence sequence) {
		// TODO Auto-generated method stub
		if(sequence.isReceiveConnection()|| sequence.isSendConnection()){
			stopAction();
		} else {
			setStopActionEnable();	
		}
	}

}
